/**
 * 
 */
package com.nari.baseinfo.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.ICity;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IHeatStation;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBaseStatisService;
import com.nari.baseinfo.service.ICityService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IHeatStationService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.baseinfo.utils.BaseStatisConstants;
import com.nari.baseinfo.utils.statisentity.IGenericStatisEntity;
import com.nari.baseinfo.utils.statisentity.ProjectStatisEntity;
import com.nari.basic.model.IUser;
import com.nari.basic.service.IUserService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class ProjectAction extends JSONActionSupport {
	private static final long serialVersionUID = -2021174383727373729L;
	private IProjectService iProjectService;
	private ICityService iCityService;
	private IHeatStationService iHeatStationService;
	private ICommunityService iCommunityService;
	private IUserService iUserService;
	private IBaseStatisService iBaseStatisService;
	// 项目信息查询
	private String projectID;
	private String projectName;
	private String capitalCityID;
	private String provinceCityID;
	private List<ICity> provinceCityList;
	private List<ICity> capitalCityList;
	private Date projDate;
	// 分页查询
	private int pageIndex = 1; // 默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IProject> projectPage;
	private Map<String, String> pageCityMap; // 所属城市id-cityName映射表
	// 项目信息管理
	private String projectUID;
	private IProject project;
	private List<IHeatStation> heatStationList;
	private List<ICommunity> communityList;
	// 项目列表查询
	private List<IProject> projectList;
	// 项目管理员
	private List<IUser> shareAreaManagerList;
	private IUser shareAreaManager;
	private String projManagerHandler; //手工填写的项目管理员
	//统计信息
	private IGenericStatisEntity statisEntity;

	/**
	 * 分页查询项目列表
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public String queryProjectPage() throws Exception {
		// 行政机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if (provinceCityID != null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 分页结果
		this.projectPage = this.iProjectService.queryAuthorityProjectWithPageCond(projectID, projectName, 
				capitalCityID, provinceCityID, projDate, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.projectPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.projectPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		// 查询项目信息对应的行政机构映射表
		this.pageCityMap = this.iProjectService.queryReferCitysByProjList(projectPage.getList());
		return "projectPage";
	}

	/**
	 * 项目信息管理
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public String queryProjectForManage() throws Exception {
		// 项目信息
		if (this.projectUID != null && !"".equals(projectUID.trim())) {
			this.project = this.iProjectService.queryEntityById(this.projectUID.trim());
			this.provinceCityID = this.project.getProvinceCityID();
			this.capitalCityID = this.project.getCapitalCityID();
		}
		// 机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if (provinceCityID != null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		//项目管理员
		this.shareAreaManagerList = iProjectService.queryProjManagerList();
		if (shareAreaManagerList == null) {
			this.shareAreaManagerList = new ArrayList<IUser>();
		}
		return "projManage";
	}
	
	/**
	 * 新增或修改项目信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-6
	 */
	public String saveOrUpdateProject() throws Exception {
//		throw new Exception("测试");
		this.rmsg = iProjectService.saveOrUpdateProject(project, projManagerHandler);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 通过唯一标识删除项目信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-6
	 */
	public String deleteProjectByUID() throws Exception {
		this.iProjectService.deleteCascadeByProjectUID(this.projectUID);
		this.rmsg = ResponseMsg.getInstance(true, "");
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 根据所属城市，查询对应的项目列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-13
	 */
	public String queryProjectsByBelongCityID() throws Exception {
		this.projectList = this.iProjectService.queryProjectsByBelongCity(provinceCityID, capitalCityID);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.projectList));
		return Action.SUCCESS;
	}

	/**
	 * 查询项目详细资料
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-2
	 */
	public String queryProjectDetail() throws Exception {
		if (this.projectUID != null && !"".equals(projectUID.trim())) {
			this.project = this.iProjectService.queryEntityById(projectUID.trim());
			//所属城市
			this.pageCityMap = this.iProjectService.queryReferCitysByProjList(Arrays.asList(new IProject[] { this.project }));
			//项目管理员
			this.shareAreaManager = iUserService.queryEntityById(project.getShareAreaManager());
			//统计信息
			this.statisEntity = iBaseStatisService.queryBaseStatisEntity(BaseStatisConstants.statisType_projectDetail, projectUID);
			ProjectStatisEntity s = (ProjectStatisEntity) this.statisEntity;
			if("40288127478b8d090147c3fa185e150b".endsWith(projectUID)) {
				s.setRoomHeatAreaSum(1301306.5);
			}
			//所属小区
			this.communityList = iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		}
		return "projectDetail";
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize = false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService
	 *            the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCityService
	 */
	@JSON(serialize = false)
	public ICityService getiCityService() {
		return iCityService;
	}

	/**
	 * @param iCityService
	 *            the iCityService to set
	 */
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}

	/**
	 * @return the iHeatStationService
	 */
	@JSON(serialize = false)
	public IHeatStationService getiHeatStationService() {
		return iHeatStationService;
	}

	/**
	 * @param iHeatStationService
	 *            the iHeatStationService to set
	 */
	public void setiHeatStationService(IHeatStationService iHeatStationService) {
		this.iHeatStationService = iHeatStationService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize = false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService
	 *            the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iUserService
	 */
	@JSON(serialize = false)
	public IUserService getiUserService() {
		return iUserService;
	}

	/**
	 * @param iUserService the iUserService to set
	 */
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	/**
	 * @return the iBaseStatisService
	 */
	@JSON(serialize = false)
	public IBaseStatisService getiBaseStatisService() {
		return iBaseStatisService;
	}

	/**
	 * @param iBaseStatisService the iBaseStatisService to set
	 */
	public void setiBaseStatisService(IBaseStatisService iBaseStatisService) {
		this.iBaseStatisService = iBaseStatisService;
	}

	/**
	 * @return the projectID
	 */
	public String getProjectID() {
		return projectID;
	}

	/**
	 * @param projectID
	 *            the projectID to set
	 */
	public void setProjectID(String projectID) {
		this.projectID = projectID;
	}

	/**
	 * @return the projectName
	 */
	public String getProjectName() {
		return projectName;
	}

	/**
	 * @param projectName
	 *            the projectName to set
	 */
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	/**
	 * @return the capitalCityID
	 */
	public String getCapitalCityID() {
		return capitalCityID;
	}

	/**
	 * @param capitalCityID
	 *            the capitalCityID to set
	 */
	public void setCapitalCityID(String capitalCityID) {
		this.capitalCityID = capitalCityID;
	}

	/**
	 * @return the provinceCityID
	 */
	public String getProvinceCityID() {
		return provinceCityID;
	}

	/**
	 * @param provinceCityID
	 *            the provinceCityID to set
	 */
	public void setProvinceCityID(String provinceCityID) {
		this.provinceCityID = provinceCityID;
	}

	/**
	 * @return the provinceCityList
	 */
	public List<ICity> getProvinceCityList() {
		return provinceCityList;
	}

	/**
	 * @param provinceCityList
	 *            the provinceCityList to set
	 */
	public void setProvinceCityList(List<ICity> provinceCityList) {
		this.provinceCityList = provinceCityList;
	}

	/**
	 * @return the capitalCityList
	 */
	public List<ICity> getCapitalCityList() {
		return capitalCityList;
	}

	/**
	 * @param capitalCityList
	 *            the capitalCityList to set
	 */
	public void setCapitalCityList(List<ICity> capitalCityList) {
		this.capitalCityList = capitalCityList;
	}

	/**
	 * @return the projDate
	 */
	public Date getProjDate() {
		return projDate;
	}

	/**
	 * @param projDate
	 *            the projDate to set
	 */
	public void setProjDate(Date projDate) {
		this.projDate = projDate;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex
	 *            the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond
	 *            the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the projectPage
	 */
	public ResultWithPageCond<IProject> getProjectPage() {
		return projectPage;
	}

	/**
	 * @param projectPage
	 *            the projectPage to set
	 */
	public void setProjectPage(ResultWithPageCond<IProject> projectPage) {
		this.projectPage = projectPage;
	}

	/**
	 * @return the pageCityMap
	 */
	public Map<String, String> getPageCityMap() {
		return pageCityMap;
	}

	/**
	 * @param pageCityMap
	 *            the pageCityMap to set
	 */
	public void setPageCityMap(Map<String, String> pageCityMap) {
		this.pageCityMap = pageCityMap;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID
	 *            the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project
	 *            the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the heatStationList
	 */
	public List<IHeatStation> getHeatStationList() {
		return heatStationList;
	}

	/**
	 * @param heatStationList
	 *            the heatStationList to set
	 */
	public void setHeatStationList(List<IHeatStation> heatStationList) {
		this.heatStationList = heatStationList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList
	 *            the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList
	 *            the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	public List<IUser> getShareAreaManagerList() {
		return shareAreaManagerList;
	}

	public void setShareAreaManagerList(List<IUser> shareAreaManagerList) {
		this.shareAreaManagerList = shareAreaManagerList;
	}

	/**
	 * @return the shareAreaManager
	 */
	public IUser getShareAreaManager() {
		return shareAreaManager;
	}

	/**
	 * @param shareAreaManager the shareAreaManager to set
	 */
	public void setShareAreaManager(IUser shareAreaManager) {
		this.shareAreaManager = shareAreaManager;
	}

	/**
	 * @return the projManagerHandler
	 */
	public String getProjManagerHandler() {
		return projManagerHandler;
	}

	/**
	 * @param projManagerHandler the projManagerHandler to set
	 */
	public void setProjManagerHandler(String projManagerHandler) {
		this.projManagerHandler = projManagerHandler;
	}

	/**
	 * @return the statisEntity
	 */
	public IGenericStatisEntity getStatisEntity() {
		return statisEntity;
	}

	/**
	 * @param statisEntity the statisEntity to set
	 */
	public void setStatisEntity(IGenericStatisEntity statisEntity) {
		this.statisEntity = statisEntity;
	}
}
